<template>
	<view class="new-products-carousel">
		<scroll-view
			class="carousel-container"
			scroll-x
			:show-scrollbar="false"
			:enable-flex="true"
		>
			<view class="products-list">
				<view
					class="product-item"
					v-for="product in productsWithDefaults"
					:key="product.id"
					@tap="onProductTap(product)"
				>
					<view class="new-badge">
						<cl-icon name="star-fill" size="12" color="#fff"></cl-icon>
						<text class="badge-text">新品</text>
					</view>
					<view class="image-container">
						<image
							:src="
								'https://shop.sdamdyy.cn/' + product.mainPic ||
								'/static/empty/no.jpg'
							"
							class="product-image"
							mode="aspectFill"
						/>
						<view class="stock-status" v-if="product.storNum < 100">
							<text class="stock-text">仅剩{{ product.storNum }}件</text>
						</view>
					</view>
					<view class="product-info">
						<text class="product-name">{{ product.title || product.name }}</text>
						<text class="product-spec">{{ product.goodsSpec || product.spec }}</text>
						<text class="product-manufacturer">{{ product.manufacturer }}</text>
						<view class="price-area">
							<view class="price-main">
								<text class="price-symbol">¥</text>
								<text class="current-price">{{
									formatUserPrice(product.price, user, product.noDiscounts)
								}}</text>
							</view>
							<view class="sales-info" v-if="product.sales">
								<text class="sales-text">已售{{ product.sales }}件</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script lang="ts" setup>
import { computed } from "vue";
import { useCool } from "/@/cool";
import { useStore } from "/@/cool/store";
import { formatUserPrice } from "/@/utils/price";
const { user } = useStore();

const props = defineProps<{
	products: any[];
}>();

const { router } = useCool();

// 为产品添加默认值和处理
const productsWithDefaults = computed(() => {
	return props.products.map((product) => ({
		...product,
		// 添加模拟数据，实际应从后端获取
		storNum: product.storNum || Math.floor(Math.random() * 200) + 50,
		sales: product.sales || Math.floor(Math.random() * 1000) + 100,
		manufacturer: product.manufacturer || "江苏恒瑞医药股份有限公司",
		goodsSpec: product.goodsSpec || product.spec || "规格详见说明书",
	}));
});

function onProductTap(product: any) {
	router.push(`/pages/goods/detail?id=${product.id}`);
}

function formatPrice(price: any): string {
	if (!price || price === 0) return "暂无报价";
	const numPrice = Number(price);
	if (isNaN(numPrice)) return "暂无报价";
	return numPrice.toFixed(2);
}
</script>

<style lang="scss" scoped>
.new-products-carousel {
	padding: 0 16rpx;
}

.carousel-container {
	width: 100%;
	white-space: nowrap;
}

.products-list {
	display: flex;
	gap: 20rpx;
	padding: 8rpx 0 20rpx;
}

.product-item {
	position: relative;
	flex-shrink: 0;
	width: 320rpx;
	background-color: #ffffff;
	border-radius: 20rpx;
	overflow: hidden;
	transition: all 0.3s ease;
	box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.06);

	&:active {
		transform: scale(0.98);
		box-shadow: 0 6rpx 20rpx rgba(0, 0, 0, 0.1);
	}
}

.new-badge {
	position: absolute;
	top: 16rpx;
	left: 16rpx;
	display: flex;
	align-items: center;
	gap: 4rpx;
	background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
	padding: 6rpx 16rpx;
	border-radius: 24rpx;
	z-index: 10;
	box-shadow: 0 2rpx 8rpx rgba(102, 126, 234, 0.3);
}

.badge-text {
	font-size: 22rpx;
	color: #ffffff;
	font-weight: 600;
}

.image-container {
	position: relative;
	width: 100%;
	height: 320rpx;
	overflow: hidden;
}

.product-image {
	width: 100%;
	height: 100%;
	background-color: #f8f8f8;
}

.stock-status {
	position: absolute;
	bottom: 12rpx;
	right: 12rpx;
	background-color: rgba(255, 165, 0, 0.9);
	backdrop-filter: blur(10rpx);
	padding: 4rpx 12rpx;
	border-radius: 20rpx;
}

.stock-text {
	font-size: 20rpx;
	color: #ffffff;
	font-weight: 500;
}

.product-info {
	padding: 20rpx;
}

.product-name {
	display: block;
	font-size: 30rpx;
	color: #1a1a1a;
	font-weight: 600;
	line-height: 1.5;
	margin-bottom: 8rpx;
	height: 90rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	display: -webkit-box;
	-webkit-line-clamp: 2;
	-webkit-box-orient: vertical;
}

.product-spec {
	display: block;
	font-size: 24rpx;
	color: #666;
	margin-bottom: 6rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.product-manufacturer {
	display: block;
	font-size: 22rpx;
	color: #999;
	margin-bottom: 16rpx;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.price-area {
	display: flex;
	align-items: flex-end;
	justify-content: space-between;
}

.price-main {
	display: flex;
	align-items: baseline;
}

.price-symbol {
	font-size: 24rpx;
	color: #ff4757;
	font-weight: 600;
}

.current-price {
	font-size: 36rpx;
	font-weight: 700;
	color: #ff4757;
	margin-left: 2rpx;
}

.sales-info {
	background-color: #f8f8f8;
	padding: 4rpx 12rpx;
	border-radius: 20rpx;
}

.sales-text {
	font-size: 20rpx;
	color: #999;
}
</style>
